package com.nny.alarm;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.nny.alarm.widget.ViewPager;
import com.uxwine.cmm.BaseFragment;
import com.uxwine.cmm.BaseFragmentActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class GuideActivity extends BaseFragmentActivity
{
	public static final int GUIDE_EDIT		= 1;
	public static final int GUIDE_LIST		= 2;
	
	static final String KEY					= "guide";
	
	boolean mbFinishedOnTouch = true;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		final Intent intent = getIntent();
		int type = intent.getIntExtra(KEY, -1);
		overridePendingTransition(-1, -1);
		if(type == GUIDE_EDIT)
		{
			setupEdit();
		}
		else if(type == GUIDE_LIST)
			setContentView(R.layout.guide_list);
		else
			finish();
	}
	
	protected void setupEdit()
	{
		
		mbFinishedOnTouch = false;
		setContentView(R.layout.guide_edit);
		
		final RadioGroup rg = (RadioGroup)findViewById(R.id.guide_edit_rg);
		
		ViewPager vp = (ViewPager)findViewById(R.id.guide_edit_vp);
		vp.setAdapter(new MVPAdpater(getSupportFragmentManager()));
		
		vp.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int index) {

				((RadioButton)rg.getChildAt(index)).setChecked(true);
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2){}
			
			@Override
			public void onPageScrollStateChanged(int arg0){}
		});
		
	}

	public boolean onTouchEvent(MotionEvent event)
	{
		if(mbFinishedOnTouch)
			this.finish();
		return true;
	}

	public static final boolean guide(Activity act, int type)
	{
		final Settings settings = Settings.getInstance(act);
		
		if(type == GUIDE_EDIT)
		{
			int verCode = getVersionCode(act);
			int code = settings.getInt(Settings.KEY_VERSION_CODE, -1);
			if(verCode != code)
			{
				start(act, type);
				return true;
			}
			return false;
		}		
		if(type == GUIDE_LIST)
		{
			if(settings.getBoolean(Settings.KEY_GUIDE_LIST, false))
				return false;
			settings.set(Settings.KEY_GUIDE_LIST, true);
			start(act, type);
			return true;
		}
		return false;
	}
	
	protected static int getVersionCode(Context ctx)
	{
		try
		{
			return ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0).versionCode;
		} 
		catch (NameNotFoundException e)
		{
			return 0;
		}
	}
	
	protected static void start(Activity act, int type)
	{
		Intent intent = new Intent(act, GuideActivity.class);
		intent.putExtra(KEY, type);
		act.startActivityForResult(intent, type);
	}
	
	class MVPAdpater extends FragmentPagerAdapter implements View.OnClickListener
	{
		BaseFragment[] mPages;
		public MVPAdpater(FragmentManager fm) {
			super(fm);
			mPages = new BaseFragment[3];
			mPages[0] = LayoutFragment.create(R.layout.fragment_guide_1);
			mPages[1] = LayoutFragment.create(R.layout.fragment_guide_2);
			mPages[2] = LayoutFragment.create(R.layout.fragment_guide_3).bindEvent(R.id.guide_3_btn, this);
			//mPages[3] = LayoutFragment.create(R.layout.fragment_guide_4).bindEvent(R.id.guide_4_btn, this);
		}

		@Override
		public int getCount()
		{
			return mPages.length;
		}

		@Override
		public Fragment getItem(int arg0) {
			return mPages[arg0];
		}

		@Override
		public void onClick(View v)
		{
			final Settings s = Settings.getInstance(getBaseContext());
			int verCode = getVersionCode(getBaseContext());
			s.set(Settings.KEY_VERSION_CODE, verCode);
			setResult(RESULT_OK);
			finish();
		}
		
	}
	
	public static class LayoutFragment extends BaseFragment
	{
		int mnLayoutId;
		View mView;
		Map<Integer, View.OnClickListener> mEvents;
		
		public LayoutFragment()
		{
			super();
			mEvents = new HashMap<Integer, View.OnClickListener>();
		}
		
		public static LayoutFragment create(int resId)
		{
			LayoutFragment lf = new LayoutFragment();
			lf.mnLayoutId = resId;
			return lf;
		}
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState)
		{		
			mView = inflater.inflate(mnLayoutId, null);
			Set<Entry<Integer, View.OnClickListener>> set = mEvents.entrySet();
			for(Entry<Integer, View.OnClickListener> entry : set)
			{
				setupEvent(entry.getKey(), entry.getValue());
			}
			return mView;
		}
		
		protected void setupEvent(int id, View.OnClickListener l)
		{
			if(null != mView)
			{
				mView.findViewById(id).setOnClickListener(l);
			}
		}
		
		protected LayoutFragment bindEvent(int id, View.OnClickListener l)
		{
			mEvents.put(id, l);
			return this;
		}
	}
}
